package com.fy.controller;

import com.fy.entity.EpcDict;
import com.fy.entity.PageEntity;
import com.fy.service.EpcDictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("epc")
@CrossOrigin
public class EpcDictController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(EpcDictController.class);

	@Autowired
	private EpcDictService epcDictService;



	/**
	 * 查询
	 * @param request
	 * @param page
	 * @param dict
	 * @return
	 */
	@RequestMapping("/ajax/dict/getDicts")
	@ResponseBody
	public Map<String,Object> getDicts(HttpServletRequest request, @ModelAttribute("page") PageEntity page, @ModelAttribute("dict") EpcDict dict){
		Map<String,Object> json = null;
		try{
			PageHelper.startPage(page.getCurrentPage(), page.getPageSize());
			List<EpcDict> dictList =this.epcDictService.getEpcDictListPage(dict,page);
			Map<String,Object> re = new HashMap<>();
			re.put("dicts",dictList);
			re.put("page",page);
			json = this.getJsonMap(true, "success", re);
		}catch(Exception e){
			logger.error("EpcDictController.getDicts", e);
			json = this.getJsonMap(false, "failure", null);
		}
		return  json;
	}

	@RequestMapping("/ajax/dict/getDictById")
	@ResponseBody
	public Map<String,Object> getDictById(HttpServletRequest request, @ModelAttribute("dict") EpcDict dict){
		Map<String,Object> json = null;
		try{
			EpcDict dict1 =this.epcDictService.getEpcDictById(dict);
			json = this.getJsonMap(true, "success", dict1);
		}catch(Exception e){
			logger.error("EpcDictController.getDicts", e);
			json = this.getJsonMap(false, "failure", null);
		}
		return  json;
	}

	@RequestMapping("/ajax/dict/getDictByKey")
	@ResponseBody
	public Map<String,Object> getDictByKey(HttpServletRequest request,@ModelAttribute("dict") EpcDict dict){
		Map<String,Object> json = null;
		try{
			EpcDict dict1 =this.epcDictService.getDictByKey(dict);
			json = this.getJsonMap(true, "success", dict1);
		}catch(Exception e){
			logger.error("EpcDictController.getDicts", e);
			json = this.getJsonMap(false, "failure", null);
		}
		return  json;
	}

	/**
	 * 新增/修改
	 * @param request
	 * @param dict
	 * @return
	 */
	@PostMapping("/ajax/dict/saveDict")
	@ResponseBody
	public Map<String,Object> saveDict(HttpServletRequest request, @ModelAttribute("dict") EpcDict dict) {
		Map<String,Object> json = null;
		try {
			epcDictService.editEpcDict(dict);
			json = this.getJsonMap(true, "success", null);
		} catch (Exception e) {
			logger.error("EpcDictController.saveDict", e);
			json = this.getJsonMap(false, "failure", null);
		}
		return  json;
	}

	/**
	 * 新增/修改
	 * @param request
	 * @param dict
	 * @return
	 */
	@PostMapping("/ajax/dict/editDictByKey")
	@ResponseBody
	public Map<String,Object> editDictByKey(HttpServletRequest request, @ModelAttribute("dict") EpcDict dict) {
		Map<String,Object> json = null;
		try {
			epcDictService.editEpcDictByKey(dict);
			json = this.getJsonMap(true, "success", null);
		} catch (Exception e) {
			logger.error("EpcDictController.saveDict", e);
			json = this.getJsonMap(false, "failure", null);
		}
		return  json;
	}
	/***
	 * <b>删除调用的方法</b>
	 * @param ids
	 * @return
	 */
	@PostMapping("/ajax/dict/deleteDict")
	@ResponseBody
	public Map<String,Object>  deleteDictBatch(String ids){
		Map<String, Object> json = null;
		try{
			this.epcDictService.deleteEpcDictBatch(ids);
			json = this.getJsonMap(true, "success", null);
		}catch (Exception e){
			logger.error("EpcDictController.deleteDictBatch", e);
			json = this.getJsonMap(false, "failure", null);
		}
		return  json;
	}
}
